//
// Created by denglibin on 2021/5/28.
//

#include "window.h"
#include <time.h>
#include "tetris_spirit.h"
#define WIN_W 200
#define WIN_H 400
#define w  20
static TetrisSpirit * spirit;
static int speed  = 20;
static int score = 0;
static int counter = 0;
static int map[WIN_H/w][WIN_W/w]; //地图
/**
 * 初始化
 */
static void init();
/**
 * 绘制函数
 * @param window
 */
static void draw(Window* window, SDL_Event*  event);

/**
 * 绘制形状
 */
static void drawSpirit(Window* window);
/**
 * 绘制地图
 * @param window
 */
static void drawMap(Window* window);

/**
 * 碰撞处理
 */
static void handleCrash();
/**
 * 事件处理函数
 * @param window
 * @param event
 */
static void handleEvent(Window* window, SDL_Event*  event);

/**
 * 越界判断
 * @return
 */
static int crossBorder();

/**
 * 是否到达底部
 * @return
 */
static int arriveBottom();

int main(int argc, char** argv){
    init();
    Window * window = createWindow(WIN_W, WIN_H, "Tetris");
    eventLoop(window, draw, handleEvent);
    WindowFree(window);
    free(spirit);
}

void init(){
    srand((unsigned int)time(NULL)); //用当前时间生成一个随机数种子
    for(int i = 0; i < WIN_H/w; i++){
        for(int j = 0; j < WIN_W/w; j++){
            map[i][j] = 0;
        }
    }
    spirit = TetrisSpiritCreteRandomType();
    spirit->x = WIN_W/2 - w*2;
}
void draw(Window* window, SDL_Event* event){

     //设置渲染器颜色
     SDL_SetRenderDrawColor(window->winRender, 0, 0, 0, 0);
     SDL_RenderClear(window->winRender); //用设置的颜色清除
     SDL_SetRenderDrawBlendMode(window->winRender, SDL_BLENDMODE_BLEND);//设置混合模式，支持alpha透明
     drawMap(window);
     counter++;
     if(counter >= 60 && !arriveBottom()){
             spirit->y += speed;
             counter = 0;
     }

     drawSpirit(window);
     handleCrash();
     //生效
     SDL_RenderPresent(window->winRender);
     SDL_ShowWindow(window->win);
}

void drawSpirit(Window* window){
    SDL_SetRenderDrawColor(window->winRender, 255, 255, 255, 255);
    SDL_Rect r ={0, 0, w, w};
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(spirit->flags[i][j] == 1){
                r.x = spirit->x + j * w;
                r.y = spirit->y + i * w;
                SDL_RenderDrawRect(window->winRender, &r);
            }
        }
    }
}

void drawMap(Window* window){
    SDL_SetRenderDrawColor(window->winRender, 255, 255, 255, 255);
    SDL_Rect r ={0, 0, w, w};
    int all = 0;
    for(int i = 0; i < WIN_H/w; i++){
        all = 1;
        for(int j = 0; j < WIN_W/w; j++){
            if(map[i][j] == 1){
                r.x = j * w;
                r.y = i * w;
                SDL_RenderDrawRect(window->winRender, &r);
                if(i == 0){
                    printf("game over");
                    exit(0);
                }
            }else{
                all = 0;
            }
        }
        if(all){ //当前行填满
            for(int j = 0; j < WIN_W/w; j++){
                map[i][j] = 0;//全部置为0
            }
            //上面的行下移一行
            for(int k = i; k >= 1; k--){
                for(int j = 0; j < WIN_W/w; j++){
                    map[k][j] = map[k-1][j];
                }
            }
            score++;
            printf("score:%d\n", score);
        }
    }

}


void handleEvent(Window* window, SDL_Event*  event){
    if(event->type == SDL_KEYDOWN){
        switch (event->key.keysym.sym) {
            case SDLK_a:
                spirit->x_direction = -1;
                if(!crossBorder()){
                    spirit->x += speed * spirit->x_direction;
                }
                break;
            case SDLK_d:
                spirit->x_direction = 1;
                if(!crossBorder()){
                    spirit->x += speed * spirit->x_direction;
                }
                break;
            case SDLK_s:
                if(!arriveBottom()){
                    spirit->y += speed;
                }

                break;
            case SDLK_SPACE:
                TetrisSpiritRotate(spirit);
                break;
        }
    }
}

int crossBorder(){
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(spirit->flags[i][j] == 1){
                if(spirit->x_direction == -1){
                    if(spirit->x + w * j<= 0 || map[spirit->y/w +i][spirit->x/w + j -1] == 1){
                        return 1;
                    }

                }
                if(spirit->x_direction == 1){
                    if(spirit->x + w * j + w >= WIN_W  || map[spirit->y/w +i][spirit->x/w + j +1] == 1){
                        return 1;
                    }

                }
            }
        }
    }
    return 0;
}

int arriveBottom(){
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(spirit->flags[i][j] == 1){
                if(spirit->y + w * i + w >= WIN_H || map[spirit->y/w +i +1][spirit->x/w + j] == 1){
                    return 1;
                }

            }
        }
    }
    return 0;
}

void handleCrash(){
    if(arriveBottom()){
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                if(spirit->flags[i][j] == 1){
                    map[spirit->y/w +i][spirit->x/w + j] = 1; //对应地图的位置
                }
            }
        }
        spirit->x = WIN_W/2 - w*2;
        spirit->y = 0;
        //重置类型
        spirit->type = rand()%7 + 1;
        TetrisSpiritTransformTo(spirit,spirit->type);
    }
}